//
//  HomeCellCacheTool.m
//  WGFGlobalHouses
//
//  Created by zhouhui on 15/4/20.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "HomeCellCacheTool.h"
#import "SiteMainViewModal.h"
#import "FMDB.h"


@implementation HomeCellCacheTool

static FMDatabaseQueue *_queue;


+(void)setup
{
    //获取沙盒中的数据文件名
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"siteMainView.sqlite"];
    //创建队列
    _queue = [FMDatabaseQueue databaseQueueWithPath:path];
    //创表
    [_queue inDatabase:^(FMDatabase *db) {
      BOOL result = [db executeUpdate:@"create table if not exists t_homecell(id integer primary key autoincrement, siteName text, openTime text, siteLocation text,leftTime text,imageUrl text,progress text,price text,moneyPoint text);"];
        
        if (result)
        {
            NSLog(@"创表成功");
        }
        else
        {
            NSLog(@"创建失败");
        }
    }];
}

//缓存一个首页cell
+(void)addSigleSiteMainViewModal:(SiteMainViewModal *)modal;
{
    [self setup];
    [_queue inDatabase:^(FMDatabase *db) {
        
        NSString *siteName = modal.siteName;
        NSString *openTime = modal.openTime;
        NSString *siteLocation = modal.siteLocation;
        NSString *leftTime = modal.leftTime;
        NSString *imageUrl = modal.siteImgUrl;
        NSMutableString *progress;
        if (modal.siteProess == SiteRenChou)
        {
            [progress setString:@"1"];
        }
        else if(modal.siteProess == SiteKaiPan)
        {
             [progress setString:@"2"];
            
        }else if (modal.siteProess == SiteReXiao)
        {
            [progress setString:@"3"];
            
        }else
        {
            [progress setString:@"4"];
        }
        NSString *price = modal.price;
        NSString *moneyPoint = modal.money;
        
        [db executeUpdate:@"insert into t_homecell(siteName,openTime,siteLocation,leftTime,imageUrl,progress,price,moneyPoint) values (?,?,?,?,?,?,?,?)",siteName,openTime,siteLocation,leftTime,imageUrl,progress,price,moneyPoint];
    }];
    [_queue close];
}

//缓存多个cell
+(void)addSiteMainViewModal:(NSArray *)modalArray;
{
    for (SiteMainViewModal *modal in modalArray)
    {
        [self addSigleSiteMainViewModal:modal];
    }
}

//返回缓存的所有数据
+(NSArray *)getSiteMainViewModals;
{
    __block NSMutableArray *arrM = nil;
    [_queue inDatabase:^(FMDatabase *db) {
        
        // 1.查询数据
        FMResultSet *rs = [db executeQuery:@"select * from t_homecell"];
        
        // 2.遍历结果集
        while (rs.next) {
            
            SiteMainViewModal *modal = [[SiteMainViewModal alloc]init];
           
            modal.siteName = [rs stringForColumn:@"siteName"];
            modal.openTime = [rs stringForColumn:@"openTime"];
            modal.siteLocation = [rs stringForColumn:@"leftTime"];
            modal.siteImgUrl = [rs stringForColumn:@"imageUrl"];
            NSString *str = [rs stringForColumn:@"progress"];
            
            if ([str isEqualToString:@"1"])
            {
                modal.siteProess = SiteRenChou;
            }
            else if ([str isEqualToString:@"2"])
            {
                modal.siteProess = SiteKaiPan;

            }
            else if ([str isEqualToString:@"3"])
            {
                 modal.siteProess = SiteReXiao;
            }
            else
            {
                modal.siteProess = SiteQingPan;
            }
            modal.price = [rs stringForColumn:@"price"];
            modal.money = [rs stringForColumn:@"moneyPoint"];
            [arrM addObject:modal];
        }
    }];
    [_queue close];
    return arrM;

}


@end
